import msgpasser;
from msg import *;
import sys;
import os;
import signal

name = raw_input('Enter mp name: ');
s=os.path.dirname(sys.argv[0]);
mp=msgpasser.messagePasser(s+'\demo.yaml',name,'l');
msgpasser.mp = mp;

print "--------------"
print "Cirl+C works"
print "--------------"

print "";

try:
    while(1):
        c = raw_input('Control(Send or Quit or True(Show alive) or False(Not show alive)): ');
        if c=='F':
            msgpasser.showAliveMessage = False
            print "Show alive off"
        if c=='T':
            msgpasser.showAliveMessage = True
            print "Show alive on"
        if c=='Q':
            mp.recethread.server.shutdown();
            msgpasser.sendalivethreadstate=False
            sys.exit(0);
        if c=='S':
            src=name;
            dest = []
            while True:
                try:
                    n = int(raw_input('Enter dest number: '))
                    break;
                except:
                    print "Enter a int!"
            for i in range(0, n):
                x = raw_input('Enter the dest: ')
                dest.append(x);
            data = raw_input('Data: ');
            mtype = raw_input('Type: ');
            newmsg= Message(src,dest,'time',data,mtype);
            print "sending message:";
            print newmsg;
            print mp.send(newmsg);
        if c=='P':
            print mp.messageRecord;
except KeyboardInterrupt:
    print "In handler. Wait for 5s to quit..."
    mp.recethread.server.shutdown()
    msgpasser.sendalivethreadstate=False
    sys.exit(0)
